package com.phuc.app.vuivui;

import static com.phuc.app.vuivui.utils.Constants.STR_ICON_SMILE_;

import com.phuc.app.vuivui.sqlite.StoryDto;
import com.phuc.app.vuivui.sqlite.TBL_STORY_dao;
import com.phuc.app.vuivui.utils.Utils;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ArrayListFragment extends Fragment {
	static final String START_TAG="<html><head><style>body {margin:0}</style>" +
	   		"<meta name=content=\" user-scalable=yes\"></head><body marginwidth=\"0\" marginheight=\"0\" align=\"center\" valign=\"center\">";
	static final String END_TAG="</body></html>";

	int mNum;
	
    /**
     * Create a new instance of CountingFragment, providing "num"
     * as an argument.
     */
    static ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

    
    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	// Get a story by id
    	final StoryDto story = TBL_STORY_dao.findById(ReadingActivity.listId.get(mNum));
    	final View v;
    	
    	// If type of story is text
    	if (story.getType() == 1) {
    		v = inflater.inflate(R.layout.story_text, container, false);
    		TextView txtContent = ((TextView) v.findViewById(R.id.read_txtContent));
    		TextView txtTitle = ((TextView) v.findViewById(R.id.read_txtTitle));
    		
    		// Set random image to imageview
			int random = Utils.makeRandomNumber(19);
			int id = Utils.getImageResourceFromName(STR_ICON_SMILE_ + random, this.getActivity());
			v.findViewById(R.id.read_imgIcon).setBackgroundResource(id);
    					
			// Set content to text view
			txtContent.setText(story.getContent());
			txtContent.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View view, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN && !story.isIsread()) {
						// Set story status to read
						story.setIsread(true);
						TBL_STORY_dao.update(story);
					}
					return false;
				}
			});
			
			txtTitle.setText(story.getTitle());
			txtTitle.setTextColor(Color.BLUE);
    	} else {
    		// If type of story is image
    		v = inflater.inflate(R.layout.story_image, container, false);
            WebView wv = (WebView)v.findViewById(R.id.wv);
            wv.getSettings().setBuiltInZoomControls(true);
            wv.getSettings().setSupportZoom(true); 
            wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv.setVerticalScrollbarOverlay (true);
            final View tv = v.findViewById(R.id.text);
            ((TextView)tv).setText("Đang tải ảnh ...");
            tv.setVisibility(View.VISIBLE);
            wv.setWebViewClient(new WebViewClient(){
            	@Override
            	public void onPageFinished(WebView view, String url) {
            		super.onPageFinished(view, url);
            		((LinearLayout) v.findViewById(R.id.image_layout)).removeView(tv);
            		
            		story.setIsread(true);
            		TBL_STORY_dao.update(story);
            	}
            });
            String html = Utils.convertHTML(story.getContent());
            wv.loadDataWithBaseURL("", START_TAG+html+END_TAG, "text/html", "utf-8", null);
    	}
    	
//        wv.loadUrl(list_url[mNum]);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}
